/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  StorageManager.java                                      
 *  File Contents Outline : 数据库操作封装类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.util;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.KeySlice;


public class StorageManager {

    private String keySpace;
    private String conlumnFamily;
    private String rowKey;

    public StorageManager(String keySpace, String conlumnFamily) {
        this.keySpace = keySpace;
        this.conlumnFamily = conlumnFamily;
        CassandraUtil.getInstance(keySpace);
    }

    public String getConlumnFamily() {
        return conlumnFamily;
    }

    public void setConlumnFamily(String conlumnFamily) {
        this.conlumnFamily = conlumnFamily;
    }

    public String getKeySpace() {
        return keySpace;
    }

    public void setKeySpace(String keySpace) {
        this.keySpace = keySpace;
    }

    public String getRowKey() {
        return rowKey;
    }
     public List<KeySlice> getAllInOneColumnByAnother(String column1,String name,String column2) {
        List<KeySlice> crs = null;
        try {
            crs = CassandraUtil.selectAllInOneColumnByAnother(keySpace, conlumnFamily,column1, name, column2);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return crs;
    }

    public void setRowKey(String rowKey) {
        this.rowKey = rowKey;
    }

    public void put(String column, String value) {
        try {
            CassandraUtil.update(keySpace, conlumnFamily, rowKey, column, value);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//     public void delete(String column, String value) {
//        try {
//            CassandraUtil.delete(keySpace, conlumnFamily, rowKey, column, value);
//        } catch (Exception ex) {
//            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
//        }
//    }

    public List<KeySlice> get(String column, String value) {
        List<KeySlice> crs = null;
        try {
            crs = CassandraUtil.select(keySpace, conlumnFamily, column, value);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return crs;
    }
     public List<KeySlice> getByTwoColumn(String column, String value,String column1, String value1) {
        List<KeySlice> crs = null;
        try {
            crs = CassandraUtil.selectByTwoColumn(keySpace, conlumnFamily, column, value, column1,  value1);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return crs;
    }

    public List<KeySlice> getAll() {
        List<KeySlice> crs = null;
        try {
            crs = CassandraUtil.selectAll(keySpace, conlumnFamily);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return crs;
    }
     public List<KeySlice> getAllInOneColumn(String column) {
        List<KeySlice> crs = null;
        try {
            crs = CassandraUtil.selectAllInOneColumn(keySpace, conlumnFamily,column);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return crs;
    }

    public void delete(String rowkey) {
        try {
            CassandraUtil.delete(keySpace, conlumnFamily, "key", rowkey);
        } catch (Exception ex) {
            Logger.getLogger(StorageManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void open() {
        CassandraUtil.open(keySpace);
    }

    public void close() {
        CassandraUtil.close(keySpace);
    }
}
